package com.xqh.study.netflixexampleorder.web;

import com.alibaba.fastjson.JSON;
import com.xqh.study.netflixexampleorder.dao.OrderRepository;
import com.xqh.study.netflixexampleorder.domain.Order;
import com.xqh.study.netflixexampleorder.encrypt.RSAUtils;
import com.xqh.study.netflixexampleorder.service.K8sInit;
import com.xqh.study.orderserviceapi.api.OrderServiceApi;
import com.xqh.study.orderserviceapi.dto.OrderDTO;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.*;
import io.kubernetes.client.openapi.models.V1APIGroup;
import io.kubernetes.client.openapi.models.V1NamespaceList;
import io.kubernetes.client.openapi.models.V1PodList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName OrderResource
 * @Description TODO
 * @Author xuqianghui
 * @Date 2020/1/7 10:11
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/order")
public class OrderResource implements OrderServiceApi {

    @Autowired
    private OrderRepository orderRepository;

    @Resource
    private K8sInit k8sInit;

    @PostConstruct
    public void init() {
        Order order = new Order();
        order.setId(1L);
        order.setAmount(100);
        order.setDetail("test order");
        order.setTitle("美味冰激凌");
        orderRepository.save(order);
    }

    @PostMapping("")
    public Order create(@RequestBody Order order) {
        return orderRepository.save(order);
    }

    @GetMapping("")
    public List<Order> list() {
        return orderRepository.findAll();
    }

    @GetMapping("keyPair")
    public Object keyPair() throws Exception {
        return RSAUtils.generateKeyPair();
    }

    @GetMapping("k8sInfo")
    public Object k8sInfo(){
        return getNamespace();
    }

    @PostMapping("k8sAppInfo")
    public Object k8sAppInfo(String namespace) throws ApiException {
        ApiClient defaultClient = k8sInit.getConnection();
//        NodeApi apiInstance = new NodeApi(client);
//        V1APIGroup result = apiInstance.getAPIGroup();
//        return result;

        AppsApi apiInstance = new AppsApi(defaultClient);
        V1APIGroup result = apiInstance.getAPIGroup();
        System.out.println(result);
        return result;
//        return getPodByNamePace(namespace);
    }

    public V1NamespaceList getNamespace() {
        ApiClient client = k8sInit.getConnection();
        CoreV1Api apiInstance = new CoreV1Api();
        String pretty = "true";
        V1NamespaceList namespaceList = new V1NamespaceList();
        try {
            namespaceList = apiInstance.listNamespace(pretty, null, null, null, null,
                    null, null, null, null, null);
        } catch (ApiException e) {
            System.err.println("Exception when calling CoreV1Api#listNamespace");
            e.printStackTrace();
        }
        return namespaceList;
    }

    public V1PodList getPodByNamePace(String namespace) {
        ApiClient client = k8sInit.getConnection();
        CoreV1Api apiInstance = new CoreV1Api();
        String pretty = "pretty_example";
        V1PodList result = new V1PodList();
        try {
            result = apiInstance.listNamespacedPod(namespace, pretty, null, null, null, null, null, null, null, null, null);
            System.out.println(result);
        } catch (ApiException e) {
            System.err.println("Exception when calling CoreV1Api#listNamespacedPod");
            e.printStackTrace();
        }
        return result;
    }


    @GetMapping("/{id}")
    @Override
    public OrderDTO getOrderById(@PathVariable Long id) {
        return JSON.parseObject(JSON.toJSONString(orderRepository.findById(id)), OrderDTO.class);
    }

}
